package dao

import (
	"apedge/pkg/protocol"
	"fmt"
	"time"

	"github.com/jinzhu/gorm"
)

const (
	TableApedgeNodeGroup string = "t_apedge_node_group"
)

type ApedgeNodeGroup struct {
	ID         int64      `gorm:"primary_key" json:"id"`
	OrgId      int64      `gorm:"not null" json:"orgId"`
	GroupId    int64      `gorm:"not null" json:"groupId"`
	UserId     int64      `gorm:"not null" json:"userId"`
	UserName   string     `gorm:"not null" json:"userName"`
	Name       string     `gorm:"unique;not null;describe:'节点组名字'" json:"name"`
	Describe   string     `gorm:"describe:'描述'" json:"describe"`
	Type       string     `gorm:"describe:'节点组类型'" json:"nodeGroupType"`
	NodeIds    string     `gorm:"describe:'节点组'" json:"nodeIds"`
	ServiceIds string     `gorm:"describe:'节点组'" json:"serviceIds"`
	CreatedAt  *time.Time `gorm:"not null" json:"createdAt"`
	UpdatedAt  *time.Time `gorm:"not null" json:"updatedAt"`
	DeletedAt  *time.Time `gorm:"default null" json:"deletedAt"`
}

func (ApedgeNodeGroup) TableName() string {
	return TableApedgeNodeGroup
}

func (t *ApedgeNodeGroup) Save(tx *gorm.DB) error {
	return tx.Save(t).Error
}

func (t *ApedgeNodeGroup) GetNodeGroupByName(tx *gorm.DB) (*ApedgeNodeGroup, error) {
	var item ApedgeNodeGroup

	err := tx.Where("name = ?", t.Name).Find(&item).Error
	if err != nil {
		return nil, err
	}
	return &item, err
}

func (t *ApedgeNodeGroup) GetNodeGroupNumByName(tx *gorm.DB) (int64, error) {
	var count int64

	err := tx.Model(&ApedgeNodeGroup{}).Where("name = ?", t.Name).Count(&count).Error
	if err != nil {
		return 0, err
	}
	return count, err
}

func (t *ApedgeNodeGroup) GetNodeGroupById(tx *gorm.DB) (*ApedgeNodeGroup, error) {
	var item ApedgeNodeGroup

	err := tx.Where("id = ?", t.ID).Find(&item).Error
	if err != nil {
		return nil, err
	}
	return &item, err
}

func (t *ApedgeNodeGroup) GetNodeGroupNumById(tx *gorm.DB) (int64, error) {
	var count int64

	err := tx.Model(&ApedgeNodeGroup{}).Where("id = ?", t.ID).Count(&count).Error
	if err != nil {
		return 0, err
	}
	return count, err
}

func (t *ApedgeNodeGroup) GetNodeGroupList(tx *gorm.DB, pageNum int64, pageSize int64, name string, sort string, userInfo *protocol.UserInfoAAA) ([]ApedgeNodeGroup, int64, error) {
	var list []ApedgeNodeGroup
	var temp []ApedgeNodeGroup
	var err error
	var count int64

	offset := (pageNum - 1) * pageSize
	query := tx.Table(t.TableName())
	query = query.Where("group_id = ?", userInfo.GroupId)

	name = "%" + name + "%"

	err = query.Where("name LIKE ?", name).Find(&temp).Count(&count).Error
	if err != nil {
		return nil, 0, err
	}
	// 排序
	sortMap := ParseSort(sort)
	for k, v := range sortMap {
		order := fmt.Sprintf("%s %s", k, v)
		query = query.Order(order)
	}
	err = query.Where("name LIKE ?", name).Limit(pageSize).Offset(offset).Find(&list).Error
	if err != nil {
		return nil, 0, err
	}

	return list, count, err
}

func (t *ApedgeNodeGroup) DeleteById(tx *gorm.DB) error {
	return tx.Where("id = ?", t.ID).Unscoped().Delete(&ApedgeNodeGroup{}).Error
}
